package api

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"web_api_gogf/app/model"
	"web_api_gogf/app/service"
	"web_api_gogf/library/response"
)

var SysMenuGroup = SysMenuGroupApi{}

type SysMenuGroupApi struct {
}

//菜单分组列表
func (s *SysMenuGroupApi) Index(r *ghttp.Request) {
	menuGroupList, err := service.SysMenu.GetMenuGroupList()
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	response.Json(r, 0, "加载成功", g.Map{
		"list":  menuGroupList,
		"total": len(menuGroupList),
	})
}

// 保存分组数据
func (s *SysMenuGroupApi) Save(r *ghttp.Request) {
	var data *model.SysMenuGroupsReq
	if err := r.ParseForm(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	rs, err := service.SysMenu.SaveMenuGroup(data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	rows, err := rs.RowsAffected()
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	response.Json(r, 0, "保存成功", g.Map{
		"rows": rows,
	})
}

// 删除菜单分组数据
func (s *SysMenuGroupApi) Destroy(r *ghttp.Request) {
	var groupRecIds []uint
	recGroupIds := r.Get("group_ids")
	groupRecIds = gconv.SliceUint(recGroupIds)
	if groupRecIds == nil {
		response.JsonExit(r, 1, "请选择菜单分组")
	}
	rs, err := service.SysMenu.DelMenuGroup(groupRecIds)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	rows, err := rs.RowsAffected()
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	response.Json(r, 0, "删除成功", g.Map{
		"rows": rows,
	})
}
